<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.io.File"%>
<%@ page import="java.io.*"%>
<html>
	<title>文档下载保存中转页面</title>
	<head>
	</head>
	<!--
*文件名:down_center.jsp
*实现功能:服务器上保存的文件名为按上传时间命名的
                下载时自动替换为中文文件名
*遗留问题:无格式判断功能,有待开发
-->
	<body>
		<%
			String root = getServletContext().getRealPath("/"); //得到网站绝对路径
			String flag = request.getParameter("flag");//子目录
			String filepath = "uploadfolder\\"+flag+"\\";
			String fileName = request.getParameter("filename"); //得到文件名
			fileName = fileName.trim().equals("")?"暂无图片.jpg":fileName;
			////////////增加对下载文件类型的判断 以设置投文件
			int ind = fileName.indexOf(".");
			String suffix  = ".jpg";
			suffix =ind>-1?fileName.substring(ind,fileName.length()):suffix;
			String contentType = suffix.equals(".doc")||suffix.equals(".docx")?"msword":"octet-stream";
			// 设置响应头和下载保存的文件名
			response.reset();
			response.setContentType("application/"+contentType); //windows
			//modify by cuiyi 20090928 转换输出文档名称 中文乱码
			response.addHeader("Content-Disposition", "attachment;filename=\"" + java.net.URLEncoder.encode(fileName,"UTF-8")
					+ "\""); //windows

			//新建文件输入输出流
			OutputStream output = null;
			FileInputStream fis = null;
			if (fileName == null || (fileName.trim().equals(""))|| (fileName.trim().equals("null"))) {
				try {
					//新建File对象,同样你可以选择你自己的InputStream
					File f = new File(root + "image\\NoRecord.jpg");
					//新建文件输入输出流对象
					output = response.getOutputStream();
					fis = new FileInputStream(f);
					//设置每次写入缓存大小
					byte[] b = new byte[204800];
					//out.print(f.length());
					//把输出流写入客户端
					int i = 0;
					while ((i = fis.read(b)) > 0) {
						output.write(b, 0, i);
					}
					output.flush();
				} catch (Exception ex) {
					ex.printStackTrace();
				}finally {
					if (fis != null) {
						fis.close();
						fis = null;
					}
					if (output != null) {
						output.close();
						output = null;
					}
					//response.flushBuffer();
					out.clear();
					out = pageContext.pushBody();
				}
			} else {
				try {
					//新建File对象,同样你可以选择你自己的InputStream
					File f = new File(root + filepath + fileName);
//					File f = new File(root + "image\\NoRecord.jpg");
					//新建文件输入输出流对象
					output = response.getOutputStream();
					fis = new FileInputStream(f);
					//设置每次写入缓存大小
					byte[] b = new byte[204800];
					//out.print(f.length());
					//把输出流写入客户端
					int i = 0;
					while ((i = fis.read(b)) > 0) {
						output.write(b, 0, i);
					}
					output.flush();
				} catch (Exception e) {
					if (fileName != null
							&& ((fileName.substring(fileName.length() - 3,
									fileName.length()).equalsIgnoreCase("jpg") || fileName
									.substring(fileName.length() - 3,
											fileName.length())
									.equalsIgnoreCase("gif"))|| fileName
									.substring(fileName.length() - 3,
											fileName.length())
									.equalsIgnoreCase("bmp"))) {
						try {
							//新建File对象,同样你可以选择你自己的InputStream
							File f = new File(root + "image/NoRecord.jpg");
							//新建文件输入输出流对象
							output = response.getOutputStream();
							fis = new FileInputStream(f);
							byte[] b = new byte[2048];
							int i = 0;
							while ((i = fis.read(b)) > 0) {
								output.write(b, 0, i);
							}
							output.flush();
						} catch (Exception ex) {
							out.print("该图片不存在");
							ex.printStackTrace();
						}
					} else {
						out.print("<script>alert('文件不存在');</script>");
						out.print("<script>window.history.back();</script>");
						return;
					}
					//out.print("该文档不存在");
					e.printStackTrace();
				} finally {
					if (fis != null) {
						fis.close();
						fis = null;
					}
					if (output != null) {
						output.close();
						output = null;
					}
					//response.flushBuffer();
					out.clear();
					out = pageContext.pushBody();
				}
			}
		%>
	</body>
</html>
